package com.jinjie.modules.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinjie.base.constant.CommonConstant;
import com.jinjie.exception.JinJieException;
import com.jinjie.exception.JinJieExceptionEnum;
import com.jinjie.modules.controller.dto.user.SysUserDTO;
import com.jinjie.modules.entity.SysUserEntity;
import com.jinjie.modules.mapper.SysUserMapper;
import com.jinjie.modules.service.SysUserService;
import com.jinjie.uitls.ConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserEntity> implements SysUserService {


    @Override
    public void insert(SysUserDTO dto) {
        SysUserEntity entity = ConvertUtil.convert(dto, SysUserEntity.class);
        entity.setStatus(CommonConstant.USER_STATUS_LIVELY);

        this.save(entity);
        //添加机构
        if (!dto.getDeptIds().isEmpty()) {
            log.info(dto.getDeptIds());
        }
        //添加角色
        if (!dto.getRoles().isEmpty()) {
            log.info(dto.getRoles());
        }

    }

    @Override
    public void edit(SysUserDTO dto) {
        SysUserEntity entity = this.getById(dto.getId());
        if(entity.getStatus() == 2){
            log.error("用户被禁用，请先恢复用户状态，再执行其他操作！");
            throw  new JinJieException(JinJieExceptionEnum.os);
        }
        entity = ConvertUtil.convert(dto, SysUserEntity.class);
        this.updateById(entity);

    }
}
